package com.itheima.task.listener;

import com.alibaba.fastjson.JSON;
import com.itheima.common.constants.ContentStatusConstants;
import com.itheima.common.constants.KafkaConstants;
import com.itheima.model.audit.pojos.AuditTask;
import com.itheima.task.service.DelayQueueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 审核请求监听器
 */
@Component
@Slf4j
public class AuditRequestListener {

    @Autowired
    private DelayQueueService delayQueueService;

    /**
     * 监听笔记审核请求
     */
    @KafkaListener(topics = KafkaConstants.AuditTopics.NOTE_AUDIT_REQUEST, groupId = KafkaConstants.ConsumerGroups.TASK_SERVICE)
    public void handleNoteAuditRequest(String message) {
        try {
            log.info("收到笔记审核请求: {}", message);
            
            AuditTask task = JSON.parseObject(message, AuditTask.class);
            task.setContentType(ContentStatusConstants.ContentType.NOTE);
            
            // 添加到延迟队列（延迟30秒后执行）
            delayQueueService.addDelayTask(task, 30);
            
        } catch (Exception e) {
            log.error("处理笔记审核请求失败: message={}", message, e);
        }
    }

    /**
     * 监听书评审核请求
     */
    @KafkaListener(topics = KafkaConstants.AuditTopics.REVIEW_AUDIT_REQUEST, groupId = KafkaConstants.ConsumerGroups.TASK_SERVICE)
    public void handleReviewAuditRequest(String message) {
        try {
            log.info("收到书评审核请求: {}", message);
            
            AuditTask task = JSON.parseObject(message, AuditTask.class);
            task.setContentType(ContentStatusConstants.ContentType.REVIEW);
            
            // 添加到延迟队列（延迟30秒后执行）
            delayQueueService.addDelayTask(task, 30);
            
        } catch (Exception e) {
            log.error("处理书评审核请求失败: message={}", message, e);
        }
    }

    /**
     * 监听评论审核请求
     */
    @KafkaListener(topics = KafkaConstants.AuditTopics.COMMENT_AUDIT_REQUEST, groupId = KafkaConstants.ConsumerGroups.TASK_SERVICE)
    public void handleCommentAuditRequest(String message) {
        try {
            log.info("收到评论审核请求: {}", message);
            
            AuditTask task = JSON.parseObject(message, AuditTask.class);
            task.setContentType(ContentStatusConstants.ContentType.COMMENT);
            
            // 添加到延迟队列（延迟30秒后执行）
            delayQueueService.addDelayTask(task, 30);
            
        } catch (Exception e) {
            log.error("处理评论审核请求失败: message={}", message, e);
        }
    }
}

